package org.example.controller;

import org.example.domain.User;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author lihongwei
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public ResponseEntity<User> add(@RequestBody User user){
        userService.insert(user);
        return ResponseEntity.ok(user);
    }

    @GetMapping("/get/{username}")
    public User getUserByUsername(@PathVariable("username") String username) {
        return userService.getUserByUsername(username);
    }
    @GetMapping("/get/{id}")
    public User getUserById(@PathVariable("id") Long id) {
        return userService.selectByPrimaryKey(id);
    }
    @PutMapping("/update")
    public Integer update(@RequestBody User user) {
        return userService.updateByPrimaryKeySelective(user);
    }
    @DeleteMapping("/delete/{id}")
    public Integer delete(@PathVariable("id") Long id) {
        return userService.deleteByPrimaryKey(id);
    }
}
